#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash gcc-10.3.1-2022.11-aarch64-linux.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
#yum install -y wget tar environment-modules libatomic

wget -t 40 -c -P $buildpath https://mirrors.huaweicloud.com/kunpeng/archive/compiler/kunpeng_gcc/gcc-10.3.1-2022.11-aarch64-linux.tar.gz
tar xf $buildpath/gcc-10.3.1-2022.11-aarch64-linux.tar.gz -C $buildpath
cp -r $buildpath/gcc-10.3.1-2022.11-aarch64-linux $installpath/

cat > "$installpath/gcc-10.3.1-2022.11-aarch64-linux/gcc_modulefiles" <<EOF
#%Module1.0
conflict gcc
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set GCC \$pwd
setenv GCC \$GCC
prepend-path PATH \$GCC/bin
prepend-path LD_LIBRARY_PATH \$GCC/lib64
EOF

#rm -rf $buildpath/gcc-10.3.1-2022.11-aarch64-linux.tar.gz && #rm -rf $buildpath/gcc-10.3.1-2022.11-aarch64-linux
